const getCtx = (selector, ctx = getCurrentPages()[getCurrentPages().length - 1]) => {
  const componentCtx = ctx.selectComponent(selector)

  if (!componentCtx) {
    throw new Error('无法找到对应的组件，请按文档说明使用组件')
  }

  return componentCtx
}

const $cActionSheet = (selector = '#c-actionsheet', ctx) => getCtx(selector, ctx)
const $cBackdrop = (selector = '#c-backdrop', ctx) => getCtx(selector, ctx)
const $cToast = (selector = '#c-toast', ctx) => getCtx(selector, ctx)
const $cLoading = (selector = '#c-loading', ctx) => getCtx(selector, ctx)
const $cDialog = (selector = '#c-dialog', ctx) => getCtx(selector, ctx)
const $cForm = (selector = '#c-form', ctx) => getCtx(selector, ctx)
const $cToptips = (selector = '#c-toptips', ctx) => getCtx(selector, ctx)
const $cGallery = (selector = '#c-gallery', ctx) => getCtx(selector, ctx)
const $cNotification = (selector = '#c-notification', ctx) => getCtx(selector, ctx)
const $cKeyBoard = (selector = '#c-keyboard', ctx) => getCtx(selector, ctx)
const $cSelect = (selector = '#c-select', ctx) => getCtx(selector, ctx)
const $cCalendar = (selector = '#c-calendar', ctx) => getCtx(selector, ctx)
const $startcRefresher = (selector = '#c-refresher', ctx) => getCtx(selector, ctx).triggerRefresh()
const $stopcRefresher = (selector = '#c-refresher', ctx) => getCtx(selector, ctx).finishPullToRefresh()
const $stopcLoader = (selector = '#c-refresher', ctx, isEnd) => getCtx(selector, ctx).finishLoadmore(isEnd)

/**
 * 浮点数运算
 */
const floatOpration = {
  /**
   * 加法运输，避免数据相加小数点后产生多位数和计算精度损失
   * @param {Number} num1 - 键，必传
   * @param {Number} num1 - 值，必传
   */
  add(num1, num2) {
    let baseNum, baseNum1, baseNum2;
    let precision // 精度
    try {
      baseNum1 = num1.toString().split('.')[1].length
    } catch (e) {
      baseNum1 = 0
    }
    try {
      baseNum2 = num2.toString().split('.')[1].length
    } catch (e) {
      baseNum2 = 0
    }
    baseNum = Math.pow(10, Math.max(baseNum1, baseNum2))
    precision = (baseNum1 >= baseNum2) ? baseNum1 : baseNum2
    return ((num1 * baseNum + num2 * baseNum) / baseNum).toFixed(precision)
  },
  /**
   * 减法运算，避免数据相减小数点后产生多位数和计算精度损失
   * @param {Number} num1 - 键，必传
   * @param {Number} num1 - 值，必传
   */
  sub(num1, num2) {
    let baseNum, baseNum1, baseNum2
    let precision // 精度
    try {
      baseNum1 = num1.toString().split('.')[1].length
    } catch (e) {
      baseNum1 = 0
    }
    try {
      baseNum2 = num2.toString().split('.')[1].length
    } catch (e) {
      baseNum2 = 0
    }
    baseNum = Math.pow(10, Math.max(baseNum1, baseNum2))
    precision = (baseNum1 >= baseNum2) ? baseNum1 : baseNum2
    return ((num1 * baseNum - num2 * baseNum) / baseNum).toFixed(precision)
  },
  /**
   * 乘法运算，避免数据相乘小数点后产生多位数和计算精度损失
   * @param {Number} num1 - 键，必传
   * @param {Number} num1 - 值，必传
   */
  multi(num1, num2) {
    let baseNum = 0
    try {
      baseNum += num1.toString().split('.')[1].length
    } catch (e) { }
    try {
      baseNum += num2.toString().split('.')[1].length
    } catch (e) { }
    return Number(num1.toString().replace('.', '')) * Number(num2.toString().replace('.', '')) / Math.pow(10, baseNum)
  },
  /**
   * 除法运算，避免数据相除小数点后产生多位数和计算精度损失
   * @param {Number} num1 - 键，必传
   * @param {Number} num1 - 值，必传
   */
  div(num1, num2) {
    let baseNum1 = 0
    let baseNum2 = 0
    let baseNum3, baseNum4
    try {
      baseNum1 = num1.toString().split('.')[1].length
    } catch (e) {
      baseNum1 = 0
    }
    try {
      baseNum2 = num2.toString().split('.')[1].length
    } catch (e) {
      baseNum2 = 0
    }

    baseNum3 = Number(num1.toString().replace('.', ''))
    baseNum4 = Number(num2.toString().replace('.', ''))
    return (baseNum3 / baseNum4) * Math.pow(10, baseNum2 - baseNum1)
  }
}



export {
  $cActionSheet,
  $cBackdrop,
  $cToast,
  $cLoading,
  $cDialog,
  $cForm,
  $cToptips,
  $cGallery,
  $cNotification,
  $cKeyBoard,
  $cSelect,
  $cCalendar,
  $startcRefresher,
  $stopcRefresher,
  $stopcLoader,
  floatOpration
}